package com.hsxc.cdz.controller.appController;

import java.awt.FontFormatException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsxc.cdz.base.bean.MyUser;
import com.hsxc.cdz.base.util.CheckMessage;
import com.hsxc.cdz.mgmt.user.MyUserManager;

import net.sf.json.JSONObject;

/**
 * 接受app用户注册
 *
 * @author wpfei
 */
@Controller
@RequestMapping("/account")
public class AppUserController {

    protected static Logger logger = Logger.getLogger("AppUserController");

    // 注入service实例对象
    @Autowired
    private MyUserManager myUserManager;

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    public Object register(@RequestBody String jsonObj, HttpServletRequest request, HttpSession httpSession) {
        MyUser myUser = new MyUser();
        JSONObject result = new JSONObject();
        org.json.JSONObject jsonObject;
        try {
            jsonObject = new org.json.JSONObject(jsonObj);
            logger.info("注册传入的json对象" + jsonObject);
            String tel = (String) httpSession.getAttribute("tel");
            tel = tel.trim();
            httpSession.setAttribute("tel", null);
            if (!CheckMessage.isPhone(tel)) {
                throw new FontFormatException("手机号码格式错误");
            }
            if (myUserManager.loadMyUserByMobile(tel) != null) {
                throw new FontFormatException("该手机号码已经被注册");
            }
            myUser.setTel(tel);
            if (jsonObject.has("password")) {
                logger.info("password" + jsonObject.getString("password").trim());
                myUser.setPassword(jsonObject.getString("password").trim());
            }
            //设置注册用户的基本信息
            if (jsonObject.has("regName"))
                myUser.setRegName(jsonObject.getString("regName"));
            if (jsonObject.has("gender"))
                myUser.setGender(jsonObject.getInt("gender"));
            if (jsonObject.has("birthday"))
                myUser.setBirthday(jsonObject.getString("birthday"));
            if (jsonObject.has("address"))
                myUser.setAddress(jsonObject.getString("address"));

        } catch (FontFormatException e) {
            result.put("success", false);
            result.put("message", "数据格式有误:" + e.getMessage());
            return result;
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "用户注册异常");
            return result;
        }
        myUser.setType(1);
        if ((myUserManager.insertOne(myUser) == 1)) {
            result.put("success", true);
            result.put("message", "用户注册成功");
        } else {
            result.put("success", false);
            result.put("message", "用户注册失败");
        }
        return result;
    }

    @RequestMapping(value = "/updateAppUser", method = RequestMethod.PUT)
    @ResponseBody
    public Object updateappuser(@RequestBody String jsonObj, HttpServletRequest request, HttpSession httpSession) {
        JSONObject params = JSONObject.fromObject(jsonObj);
        JSONObject result = new JSONObject();
        result.put("success", true);
        logger.debug("收到的数据" + params);
        Subject subject = SecurityUtils.getSubject();
        if (subject == null || subject.getPrincipal() == null) {
            result.put("added", false);
            result.put("errcode", MyUser.NO_LOGIN);
            result.put("message", "当前用户没有登录");
            return result;
        }
        String tel = subject.getPrincipal().toString();
        if (tel == null) {
            result.put("added", false);
            result.put("errcode", 2);
            result.put("message", "用户信息输入有误");
            return result;
        }
        MyUser myUser = new MyUser();
        //修改密码的数据
        if (params.has("password"))
            myUser.setPassword(params.getString("password"));
        if (params.has("tel"))
            myUser.setTel(params.getString("tel"));
        //获取修改的用户的基本信息
        if (params.has("regName"))
            myUser.setRegName(params.getString("regName"));
        if (params.has("gender"))
            myUser.setGender(params.getInt("gender"));
        if (params.has("birthday"))
            myUser.setBirthday(params.getString("birthday"));
        if (params.has("address"))
            myUser.setAddress(params.getString("address"));
        logger.info("json解析成功");

        myUser.setTel(tel);
        if (myUserManager.updateUserByTel(myUser) >= 0) {
            result.put("errcode", 0);
            result.put("userInfo", myUserManager.loadMyUserByMobile(tel));
            return result;
        } else {
            result.put("errcode", 1);
            result.put("message", "修改失败");
            return result;
        }

    }

    @RequestMapping(value = "/getAppUserInfo", method = RequestMethod.GET)
    @ResponseBody
    public Object getAppUserInfo() {

        JSONObject result = new JSONObject();
        result.put("success", true);

        Subject subject = SecurityUtils.getSubject();
        if (subject == null || subject.getPrincipal() == null) {
            result.put("added", false);
            result.put("errcode", MyUser.NO_LOGIN);
            result.put("message", "当前用户没有登录");
            return result;
        }
        String tel = subject.getPrincipal().toString();
        if (tel == null) {
            result.put("added", false);
            result.put("errcode", 2);
            result.put("message", "用户信息输入有误");
            return result;
        }
        MyUser myUser = myUserManager.loadMyUserByMobile(tel);
        if (myUser != null) {
            result.put("errcode", 0);
            result.put("userInfo", myUser);
            return result;
        } else {
            result.put("errcode", 1);
            result.put("message", "不存在用户");
            return result;
        }
    }
}
